<template>
  <el-drawer
      ref="drawerRef"
      v-model="dialog"
      custom-class="demo-drawer"
      direction="rtl"
      title="新增字典标签"
  >
    <div class="demo-drawer__content">
      <el-form ref="formRef" :model="form">
        <el-form-item :label-width="formLabelWidth" label="字典键值:" prop="tag">
          <el-input v-model="form.key" autocomplete="off"/>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="字典标签:" prop="key">
          <el-input v-model="form.tag" autocomplete="off"/>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="字典内容:" prop="group">
          <el-input v-model="form.value" autocomplete="off"/>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="字典组:" prop="group">
          <el-input v-model="form.group" autocomplete="off"/>
        </el-form-item>
        <el-form-item>
          <el-button size="large" style="width: 100%;margin-top: 20px" type="primary" @click="submit(form)">提交
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button size="large" style="width: 100%;" type="primary" @click="resetForm(formRef)">清空</el-button>
        </el-form-item>
      </el-form>
    </div>
  </el-drawer>
</template>

<script lang="ts" setup>
import {reactive, ref} from 'vue'
import type {FormInstance} from 'element-plus'
import {ElDrawer} from 'element-plus'
import {AddDictApi} from "@/api/Dict";

const formLabelWidth = '80px'
let timer

const table = ref(false)
const dialog = ref(false)
const loading = ref(false)

const form = reactive({
  id: 0,
  key: '',
  tag: '',
  group: '',
  value: ''
})

const formRef = ref<FormInstance>()
const drawerRef = ref<InstanceType<typeof ElDrawer>>()

const emits = defineEmits(["DMFormClick"]);
const SubmitForm = () => {
  emits('DMFormClick')
}


const submit = (form) => {
  AddDictApi(form.id, form.key, form.value, form.tag, form.group).then(function (res) {
    if (res['code'] != 1002) {
      SubmitForm()
      CloseDrawer()
    } else {
      alert(res['data'])
    }

  })
}
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}

const onClick = () => {
  drawerRef.value!.close()
}
const OpenDrawer = (row) => {

  dialog.value = true
}
const CloseDrawer = () => {
  onClick()
}
defineExpose({
  OpenDrawer,
});


</script>
